package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.github.pagehelper.Page;
import com.rs.model.ZhDepartment;
import com.rs.model.ZhPosition;
import com.rs.service.ZhDepartmentService;
import com.rs.service.ZhPositionService;
import com.rs.util.SpringContextUtils;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import javax.swing.JTabbedPane;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedHashSet;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;

public class ZhDepartmentTextAdd extends JFrame {
	private JTextField textField;
	private JTextField textField_1;
	
	private ZhDepartmentService departmentService = SpringContextUtils.getBean(ZhDepartmentService.class);
	
	private ZhPositionService positionService = SpringContextUtils.getBean(ZhPositionService.class);
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ZhDepartmentTextAdd frame = new ZhDepartmentTextAdd(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ZhDepartmentTextAdd(final JDesktopPane desktopPane_1) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				ZhDepartmentList zhDepartmentList = new ZhDepartmentList(desktopPane_1);
				zhDepartmentList.setVisible(true);
				desktopPane_1.add(zhDepartmentList);
			}
		});
		setTitle("添加");
		setIconImage(Toolkit.getDefaultToolkit().getImage(ZhDepartmentTextAdd.class.getResource("/images/logo1.png")));
		setBounds(100, 100, 232, 236);
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 216, 198);
		getContentPane().add(tabbedPane);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("部门", null, panel, null);
		panel.setLayout(null);
		
		final JLabel lblNewLabel = new JLabel("部门名称：");
		lblNewLabel.setBounds(24, 59, 68, 15);
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(112, 56, 66, 21);
		panel.add(textField);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("提交");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZhDepartment zhDepartment = new ZhDepartment();
				zhDepartment.setDeName(textField.getText());
				int a = departmentService.insertSelective(zhDepartment);
				if(a == 0) {
					showMessageDialog("部门已存在");
				}else {
					showMessageDialog("提交成功");
				}
			}
		});
		btnNewButton.setBounds(63, 113, 93, 23);
		panel.add(btnNewButton);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("职务", null, panel_1, null);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("部门名称：");
		lblNewLabel_1.setBounds(32, 25, 66, 15);
		panel_1.add(lblNewLabel_1);
		
		
		JLabel lblNewLabel_2 = new JLabel("职务名称：");
		lblNewLabel_2.setBounds(31, 63, 66, 15);
		panel_1.add(lblNewLabel_2);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(getDepartmentData()));
		comboBox.setBounds(107, 23, 79, 21);
		panel_1.add(comboBox);
		
	
		
		textField_1 = new JTextField();
		textField_1.setBounds(107, 60, 79, 21);
		panel_1.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("是否管理层人员：");
		lblNewLabel_3.setBounds(10, 97, 107, 15);
		panel_1.add(lblNewLabel_3);
		
		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"是", "否"}));
		comboBox_1.setBounds(120, 91, 66, 21);
		panel_1.add(comboBox_1);
		
		
		JButton btnNewButton_1 = new JButton("提交");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZhPosition zhPosition = new ZhPosition();
				zhPosition.setDepName((String)comboBox.getSelectedItem());
				zhPosition.setPoName(textField_1.getText());
				String comboBox_1s = (String)comboBox_1.getSelectedItem();
				if(comboBox_1s.equals("是")) {
					zhPosition.setPo_type(0);
				}else {
					zhPosition.setPo_type(1);
				}
				int a = positionService.insertSelective(zhPosition);

				if(a == 0) {
					showMessageDialog(""
							+ "该部门职务已存在");
				}else {
					showMessageDialog("提交成功");
				}
							}
		});
		btnNewButton_1.setBounds(62, 136, 93, 23);
		panel_1.add(btnNewButton_1);
		
	

	}
	
	//获取所有部门名称
	public String[] getDepartmentData(){
		//所有的部门信息
		List<ZhDepartment> dataList =	departmentService.selectByExample();
		//获取数据 
		 LinkedHashSet<String> hs = new LinkedHashSet<String>();
		for (ZhDepartment zhDepartment : dataList) {
			hs.add(zhDepartment.getDeName());
		}
		String[] typeTeacherStrings =  new String[hs.size()];
		int i = 0;
		for (String string : hs) {
				typeTeacherStrings[i] = string;
				i++;
		}
		return typeTeacherStrings;
	}
	
	//提示弹窗
	public void showMessageDialog(String massage){
		JOptionPane.showMessageDialog(this,massage);
	}
}
